<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Kernel\Traits\DaoTrait;
use App\Model\Coupon;

/**
 * 折扣券 DAO
 *
 * @author 刘兴永(aile8880@qq.com)
 * @package App\Dao
 */
class CouponDao extends Dao
{
    use DaoTrait;

    /**
     * 订单状态-完成
     *
     * @var int
     */
    const STATUS_COMPLETED = 2;

    /**
     * 全店打折
     *
     * @var int
     */
    const SHOP_DISCOUNT = 3;

    /**
     * 优惠券
     *
     * @var int
     */
    const COUPON = 1;

    /**
     * 现金抵用券
     *
     * @var int
     */
    const ENABLE_VOUCHER = 2;
    /**
     * Get model.
     *
     * @return string
     */
    public function model(): string
    {
        return Coupon::class;
    }

    /**
     * 获取优惠券列表
     * @param int $type 类型
     * @param array $map 查询条件
     * @param string|null $sort 排序方式
     * @return mixed
     */
    public function getList(int $type, array $map, string $sort)
    {
        return Coupon::query()
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('status', 1)
            ->where('type', $type)
            ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture', 'sales', 'stock',  'start_time','expire_time','instructions')
            ->orderByRaw($sort)
            ->paginate(10);
    }

    /**
     * 优惠券详情
     * @param array $map
     * @return mixed
     */
    public function CouponDetails(array $map)
    {
        return Coupon::query()
            ->with('business:id,name,logo,address,tel,forenoon_hours,afternoon_hours,hours_describe')
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->select('id', 'business_id', 'picture', 'title', 'sales', 'stock', 'goods_price', 'price', 'reach_price', 'description', 'expire_time', 'expire_type', 'created_at as created_time')
            ->first();
    }

    /**
     * 删除优惠券
     *
     * @param array $map 匹配条件
     * @return int
     */
    public function delete(array $map)
    {
        return Coupon::query()->where(function ($query) use ($map) {
            return queryWhere($query, $map);
        })->delete();
    }

    /**
     * 通过条件查询单条数据
     *
     * @param array $map
     * @return mixed
     */
    public function getCouponByMap(array $map)
    {
        return Coupon::query()
            ->where(function ($query) use ($map) {
            return queryWhere($query, $map);
        })
         ->select('*')
         ->first();
    }

    /**
     * 通过条件查询全店入驻数据
     *
     * @param array $map
     * @return mixed
     */
    public function getShopDiscountList(array $map , string $sort)
    {
        return Coupon::query()
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('status', 1)
            ->select('id', 'business_id', 'type', 'title', 'picture','category_id')
            ->orderByRaw($sort)
            ->paginate(10);
    }

    /**
     * 根据类型查询券的列表
     *
     * @param int $type
     * @param array $map
     * @param string $sort
     * @return \Hyperf\Contract\LengthAwarePaginatorInterface
     */
    public function getCouponListByType (int $type, array $map, string $sort)
    {
        return Coupon::query()
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('status', 1)
            ->where('type', $type)
            ->select('*')
            ->orderByRaw($sort)
            ->paginate(10);
    }

}